Handling Moodle error messages (ENT-9080)#2563
Handling Moodle error messages (ENT-9080)#2563shravani-sonata-gottapu wants to merge 4 commits intoopenedx:masterfrom
Conversation
0240c33 to
f56244d
Compare
rgopalrao-sonata-png
left a comment
There was a problem hiding this comment.
Please check the comments and update
|
|
||
| # Define mapped_status based on error_code | ||
| mapped_status = { | ||
| 'invalidtoken': 'Invalid Token', | ||
| 'missingfield': 'Missing Field', | ||
| 'duplicatedata': 'Duplicate Data', | ||
| }.get(error_code, 'Unknown Error') | ||
|
|
There was a problem hiding this comment.
If this is moodle response :HTTP/1.1 200 OK
Content-Type: application/json
{
"errorcode": "missingparam",
"message": "A required parameter is missing."
}
This will still return elif error_code:
raise MoodleClientError(
'Moodle API Client Task "{method}" failed with error code '
'"{code}" and message: "{msg}" '.format(...),
response.status_code, # ( This is will return 200 status code)!
moodle_error={'error_code': error_code, ...}
)
There was a problem hiding this comment.
You can have a function like to correct the status
def _effective_error_status_code(status_code, error_code=None):
"""Map Moodle error codes or convert false-200s to failure statuses"""
if error_code and error_code in MOODLE_ERROR_STATUS_MAP:
return MOODLE_ERROR_STATUS_MAP[error_code]
if status_code is None or status_code < 400:
return 555 # Semantic error
return status_code
There was a problem hiding this comment.
elif error_code:
status_code = _effective_error_status_code(response.status_code, error_code)
raise MoodleClientError(..., status_code, moodle_error={...}) u can call the function to change the status code
| from integrated_channels.integrated_channel.client import IntegratedChannelApiClient | ||
| from integrated_channels.utils import generate_formatted_log, stringify_and_store_api_record | ||
|
|
||
| MOODLE_ERROR_STATUS_MAP = { |
There was a problem hiding this comment.
Please look into this https://2u-internal.atlassian.net/wiki/spaces/SOL/pages/edit-v2/1081409695?draftShareId=33d153f3-4beb-45a9-a403-aee6f781e5e1
MOODLE_ERROR_STATUS_MAP = {
# Duplicate/Conflict errors
"shortnametaken": 409,
"courseidnumbertaken": 409,
"morethanonerecordinfetch": 409,
# Not Found errors
"cannotfindcourse": 404,
"cannotfinduser": 404,
"invalidrecord": 404,
"usernamenotfound": 404,
# Bad Request errors
"missingparam": 400,
"codingerror": 400,
# Authentication/Authorization errors
"invalidlogin": 401,
"invalidtoken": 401,
"accessexception": 403,
"errorcatcontextnotvalid": 403,
"usernotfullysetup": 403,
# Server/Database errors
"dbconnectionfailed": 503,
"dmlreadexception": 503,
}Needs to be handled.
f32eab4 to
2b186b8
Compare
rgopalrao-sonata-png
left a comment
There was a problem hiding this comment.
Updated code and resolved
|
@shravani-sonata-gottapu I don't have deep expertise on integrated channels stuff, but would it make more sense to include this change in https://github.com/openedx/enterprise-integrated-channels/blob/main/channel_integrations/moodle/client.py ? |
Description :
Moodle returns 200 response status code on failed transmission jobs which is the historical and default behavior of Moodle web services.
Changes :
Added 400 responses and mapped them.
Testing :
Local testing is not possible due to unavailability of data. we can test directly in stage/prod to verify the errors.